package rbb.itm5.bwinf.roboter.View;

import com.apple.eawt.Application;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
import rbb.itm5.bwinf.roboter.Control.ControlerEvents;

public class Gui implements GuiInterface {

    private JFrame frame = new JFrame("Roboter");
    private Room room;
    private ControlPanel ControlPanel;
    private JScrollPane pane;

    public Gui(ControlerEvents event) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        room = new Room(event);
        ControlPanel = new ControlPanel(event);
        pane = new JScrollPane(room.getView(), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    }

    @Override
    public void initGui(int width, int height) {
        frame.setSize(220 + (room.getOffset() * 2 + (width * rbb.itm5.bwinf.roboter.Model.Room.GridSize)), (room.getOffset() * 2) + (height * rbb.itm5.bwinf.roboter.Model.Room.GridSize) + 40);
        if (!frame.isVisible()) {
            frame.setLocationByPlatform(true);
        }
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        //frame.setResizable(false);

        ControlPanel.getView().setPreferredSize(new Dimension(200, 0));
        room.getView().setLayout(new BorderLayout());
        room.getView().setPreferredSize(new Dimension(width * rbb.itm5.bwinf.roboter.Model.Room.GridSize + room.getOffset() * 2, height * rbb.itm5.bwinf.roboter.Model.Room.GridSize + room.getOffset() * 2));
        room.getView().setBorder(new LineBorder(Color.BLACK,2));

        pane.getVerticalScrollBar().setUnitIncrement(15);
        pane.getHorizontalScrollBar().setUnitIncrement(15);
        if (pane.getViewport().getComponentListeners().length == 0) {
            pane.getViewport().addComponentListener(new ComponentAdapter() {
                @Override
                public void componentResized(ComponentEvent e) {
                    JViewport viewport = (JViewport) e.getSource();
                    viewport.validate();
                }
            });
        }
        AdjustmentListener listener = new AdjustmentListener() {
            boolean centered = false;

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                if (!centered) {
                    return;
                }
                JScrollBar bar = (JScrollBar) e.getSource();
                if (bar == pane.getVerticalScrollBar()) {
                    bar.setValue((bar.getMaximum() - pane.getViewport().getSize().height) / 2);
                    //System.out.println("Y="+bar.getValue()+" max="+(bar.getMaximum()-pane.getViewport().getSize().height)+" min="+bar.getMinimum());
                } else {
                    bar.setValue((bar.getMaximum() - pane.getViewport().getSize().width) / 2);
                    //System.out.println("X="+bar.getValue()+" max="+bar.getMaximum()+" min="+bar.getMinimum());
                }
            }
        };
        pane.getVerticalScrollBar().addAdjustmentListener(listener);
        pane.getHorizontalScrollBar().addAdjustmentListener(listener);
        
        validate();
        room.getView().requestFocus();
    }

    @Override
    public void buildGui() {
        frame.getContentPane().removeAll();
        frame.getContentPane().add(ControlPanel.getView(), BorderLayout.WEST);
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        
        
        
        
        if (System.getProperty("os.name").toUpperCase().startsWith("MAC")) {
            Application app = Application.getApplication();
            Image img = new ImageIcon("icons/cellbot-icon.png").getImage();
            SystemTray tray = SystemTray.getSystemTray();
            TrayIcon trayIcon = new TrayIcon(img, "Icon");

            try {
                tray.add(trayIcon);
            } catch (AWTException e1) {
            }


            // Dock-Icon setzen
            app.setDockIconImage(img);
        } else {
            ImageIcon icon = new ImageIcon("icons/cellbot-icon.png");
            frame.setIconImage(icon.getImage());
        }
        
        
        frame.setTitle("Roboter");
    }

    @Override
    public void start() {
        frame.setVisible(true);
    }

    @Override
    public void setSize(Dimension size) {
        frame.setSize(size);
    }

    public Room getRoom() {
        return room;
    }

    public void repaint() {
        pane.getViewport().setSize(room.getView().getSize());
        room.getView().repaint();
    }

    public ControlPanel getControlPanel() {
        return ControlPanel;
    }

    public void validate() {
        pane.getViewport().setSize(room.getView().getSize());
        frame.getContentPane().validate();
        pane.validate();
        frame.validate();
    }

    /*
    public void setMacTrayIcon(String FileName) {
        Application app = Application.getApplication();
        Image img = new ImageIcon(FileName).getImage();
        SystemTray tray = SystemTray.getSystemTray();
        TrayIcon trayIcon = new TrayIcon(img, "Icon");

        try {
            tray.add(trayIcon);
        } catch (AWTException e1) {
        }
        

        // Dock-Icon setzen
        app.setDockIconImage(img);
    }
    */
    public Component getJFrame() {
        return frame;
    }
}
